package com.CityHunter.Request;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.util.Log;

import com.CityHunter.Common.Http;
import com.google.android.maps.GeoPoint;


public class GetBuddyGeoPointRequest {

	private String getBuddyGeoPointPage;

	public GetBuddyGeoPointRequest(String getBuddyGeoPointPage) {
		this.getBuddyGeoPointPage = getBuddyGeoPointPage;
	}

	public GeoPoint getBuddyGeoPoint(int buddyid) {
		
		GeoPoint buddyGeoPoint = null;
		
		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("buddyid", Integer.toString(buddyid)));

		// Send Get Buddy ID Request
		Http http = new Http();
		Log.d("abdy_Debug", "Sending Request : " + getBuddyGeoPointPage);
		JSONArray jsonResponse = http.sendRequest(getBuddyGeoPointPage, params);
		try {
			double geoLatitude = Double.parseDouble(jsonResponse.getJSONObject(0)
					.getString("lastLatitude"));
			double geoLongitude = Double.parseDouble(jsonResponse.getJSONObject(0)
					.getString("lastLongitude"));
			buddyGeoPoint = new GeoPoint((int)(geoLongitude*1E6),(int)(geoLatitude*1E6));
			Log.d("abdy_Debug", "Response BuddyID : " + buddyid);
		} catch (Exception e) {
			Log.e("abdy_Error", e.getMessage().toString());
		}
		
		return buddyGeoPoint;
	}

}
